import Taro from "@tarojs/taro";

// export const baseUrl = "https://mp.choryoung.com/cdims/";
export const baseUrl = "http://192.168.1.112:8100/";
// export const baseUrl = "http://192.168.1.112:8099/";
const request = (
  url: string,
  options: {
    method: Taro.request.Option["method"];
    data: Taro.request.Option["data"];
  },
  isFormData?: boolean
) => {
  return new Promise((resolve, reject) => {
    const token = Taro.getStorageSync("token");
    Taro.request({
      url: baseUrl + url,
      method: options.method,
      data: options.data,
      header: {
        "content-type": isFormData
          ? "application/x-www-form-urlencoded"
          : "application/json",
        Authorization: token,
        wxToken: token
        // cookie: "JSESSIONID=15302B1F20D0992172E9B26231DDDCA9"
      },
      success(res) {
        if (res.data.code === "200000") {
          resolve(res.data.data);
        }
        if (!res.data.retData) {
          resolve(res.data);
        }
        resolve(res.data.retData);
      },
      fail(error) {
        reject(error.errMsg);
      }
    });
  });
};

const originRequest = (
  url: string,
  options: {
    method: Taro.request.Option["method"];
    data: Taro.request.Option["data"];
  },
  isFormData?: boolean
) => {
  return new Promise((resolve, reject) => {
    const token = Taro.getStorageSync("token");
    Taro.request({
      url: baseUrl + url,
      method: options.method,
      data: options.data,
      header: {
        "content-type": isFormData
          ? "application/x-www-form-urlencoded"
          : "application/json",
        Authorization: token,
        wxToken: token
        // cookie: "JSESSIONID=15302B1F20D0992172E9B26231DDDCA9"
      },
      success(res) {
        resolve(res.data.retData);
      },
      fail(error) {
        reject(error.errMsg);
      }
    });
  });
};

const get = <T extends {}>(url: string, options?: T) => {
  return request(url, { method: "GET", data: options });
};
const post = <T extends {}>(url: string, options: T) => {
  // console.log(`${app.globalData.globalUrl}`);
  return request(url, { method: "POST", data: options });
};

const formDataPost = <T extends {}>(url: string, options: T) => {
  // console.log(`${app.globalData.globalUrl}`);
  return request(url, { method: "POST", data: options }, true);
};

const put = (url: string, options = {}) => {
  return request(url, { method: "PUT", data: options });
};
// 不能声明DELETE（关键字）
const remove = (url: string, options = {}) => {
  return request(url, { method: "DELETE", data: options });
};

export const OriginRequest = {
  request: originRequest
};

export const CustomRequest = {
  request,
  get,
  post,
  formDataPost,
  put,
  remove
};

export const countPageTime = (duration: number, pageType: number) => {
  CustomRequest.post("/appletPageVisitSts/wx/save", {
    duration,
    pageType
  });
};

export enum PageType {
  "集团首页" = 1,

  "佳园会活动二级页面" = 2,

  "项目一级页面" = 3,

  "壹号院" = 4,

  "信园" = 5,

  "润园" = 6,

  "商业街" = 7,

  "推荐页面" = 8,

  "去推荐页面" = 9
}

export enum PageProjectType {
  "yihaoyuan" = 4,

  "xinyuan" = 5,

  "runyuan" = 6,

  "shangyejie" = 7
}

export enum PageProjectTypeIndeed {
  "集团首页" = 1,

  "佳园会活动二级页面" = 2,

  "项目一级页面" = 3,

  "yihaoyuan" = 4,

  "xinyuan" = 5,

  "runyuan" = 6,

  "shangyejie" = 7,

  "推荐页面" = 8,

  "去推荐页面" = 9
}
